import requests

def send_message_to_fastapi(user_id, user_input, sys_prompt="你是一个有用的助手。", 
                            history=[], history_len=1, 
                            temperature=0.5, top_p=0.5, max_tokens=None):
    url = "http://127.0.0.1:6066/chat"
    payload = {
        "user_id": user_id,
        "query": user_input,
        "sys_prompt": sys_prompt,
        "history_len": history_len,
        "temperature": temperature,
        "top_p": top_p,
        "max_tokens": max_tokens
    }
    headers = {
        "Content-Type": "application/json"
    }
    response = requests.post(url, json=payload, headers=headers, stream=True)
    
    if response.status_code == 200:
        for chunk in response.iter_content(chunk_size=8192, decode_unicode=True):
            if chunk:
                print(chunk, end='', flush=True)
    else:
        print(f"Error: {response.status_code}")

if __name__ == "__main__":
    user_id = "123"
    while True:
        user_input = input("\n请输入您的问题（输入'退出'结束对话）：")
        if user_input.lower() == '退出':
            break
        send_message_to_fastapi(user_id, user_input)
        
